<?php
/**
 *file-level documentation
 *@author Name <email@exp.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, Tuncay Bakan
 *@package somePackage
 *
 */
/**
 * Provides static methods to load and instanciate controllers, models and files
 *
 */
class Loader{

    /**
     * Loads the controller with the passed name
     *
     * @param String $sName The name of the controller that shall be instanciated
     * @return Object Controller returns an instance of a Controller with the passed name
     */
    public static function loadController($sName) {
        //the postfix of a controller class, every controller class has a name like 'ControllerName_Controller'
        $sIdentifier = '_Controller';

        $sControllerPath = CMS_ROOT . 'content/controller/';
        $sSuffix = '.class.php';
        $sFileName = $sControllerPath . CMS_PREFIX . $sName . $sSuffix;
        $sClass = ucfirst($sName). $sIdentifier;

        //if the file does not exist or the the class is not defined an instance
        //of the default-controller is returned
        //else, an instance of the controller with the passed name is instanciated

        if((isset($_SESSION['username'])||($sName == 'login'))){
            if((self::checkFile($sFileName)&&self::checkClass($sClass))){
                //return an instance of the constructor with the passed name
                return new $sClass;
            }else{
                //instantiate default controller
                if(file_exists($sControllerPath . CMS_PREFIX . CMS_DEFAULT_CONTROLLER . $sSuffix)){
                    return self::loadController(CMS_DEFAULT_CONTROLLER);
                }else {
                    echo "DC does not exists";
                }
            }
            //user is not logged in, load login controller
        } else{

            if(file_exists($sControllerPath . CMS_PREFIX . 'login' . $sSuffix)){
                return Loader::loadController('login');
            }else {
                echo "login does not exists";
            }
        }
    }

    /**
     * loads the Model with the passed name
     *
     * @param String $sName The name of the model that shall be instanciated
     * @return Object Model returns an instance of a model with the passed name
     */
    public static function loadModel($sName) {
        //the postfix of a controller class, every controller class has a name like 'ControllerName_Controller'
        $sIdentifier = '_Model';

        $sModelPath = CMS_ROOT . 'content/model/';
        $sSuffix = '.php';
        $sFileName = $sModelPath . CMS_PREFIX . $sName . $sSuffix;
        $sClass = ucfirst($sName). $sIdentifier;

        //if the file does not exist or the the class is not defined an instance
        //of the default-controller is returned
        //else, an instance of the controller with the passed name is instanciated

        if(!(self::checkFile($sFileName)&&self::checkClass($sClass))){
            //recursive call to load the default controller
            if(file_exists($sModelPath . CMS_PREFIX . CMS_DEFAULT_MODEL . $sSuffix)){
                return self::loadModel(CMS_DEFAULT_MODEL);
            }else{
                echo "DM does not exists";
            }
        } else {
            //return an instance of the constructor with the passed name
            return new $sClass;
        }
    }

    /**
     * Loads a class with the given name
     * @param string $sName
     */
    public static function loadClass($sName){
        //construct class name
        $sClassPath = CMS_ROOT . 'content/lib/';
        $sSuffix = '.class.php';
        $sFileName = $sClassPath . CMS_PREFIX . strtolower($sName) . $sSuffix;
        $sClass = ucfirst($sName);


        if(!(self::checkFile($sFileName)&&self::checkClass($sClass))){
            echo "The class does not exists";
        }
    }

    /**
     * Includes a file
     * @param string $sFilePath
     */
    public static function loadFile($sFilePath){
        //call checkFile to validate and include the file
        return self::checkFile($sFilePath);
    }

    /**
     * Checks if file exists
     *
     * @param String $sFileName
     * @return boolean does the file exist?
     */
    private static function checkFile($sFilePath){
        if(file_exists($sFilePath)){
            //this line includes the passed file
            //and returns the return value of the include_once method
            //if include_once is successful it returns "1"
            return include_once($sFilePath);
        } else {
            return false;
        }
    }

    /**
     * Checks if class exists.
     * can be called only after the file of the class was included before
     *
     * @param String $sClass the name of the class
     * @return boolean does the class exist?
     */
    private static function checkClass($sClass){
        //only possible if checkFile() or include_once() was called before
        if(class_exists($sClass)){
            return true;
        }
        else {
            return false;
        }
    }
}
?>